package com.imooc.example.ticket;

import com.imooc.example.ticket.event.saga.StudentAgedEvent;
import com.imooc.example.ticket.command.StudentAgeCommand;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.model.AggregateIdentifier;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.spring.stereotype.Aggregate;

import static org.axonframework.commandhandling.model.AggregateLifecycle.apply;

@Slf4j
@Aggregate
public class StudentAge {

    @AggregateIdentifier
    private String id;

    private String name;
    private Integer age;

    public StudentAge() {
    }

    @CommandHandler
    public StudentAge(StudentAgeCommand command) {
        StudentAgedEvent event = StudentAgedEvent.builder()
                .ageId(command.getAgeId())
                .studentId(command.getStudentId())
                .name(command.getName())
                .age(command.getAge()).build();
        apply(event);
        log.info("StudentAgeCommand = {}", event);
    }

    @EventSourcingHandler
    public void onAge(StudentAgedEvent event) throws InterruptedException {
        this.id = event.getAgeId();
        this.name = event.getName();
        this.age = 18;
        log.info("赋值年龄: {}", age);
    }


}
